package com.ljh.onlinetest.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljh.onlinetest.domain.Message;
import com.ljh.onlinetest.domain.MessageUser;
import com.ljh.onlinetest.repository.BaseMapper;
import com.ljh.onlinetest.repository.MessageMapper;
import com.ljh.onlinetest.repository.MessageUserMapper;
import com.ljh.onlinetest.service.MessageService;
import com.ljh.onlinetest.viewmodel.admin.message.MessageRequestPageVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ljh
 * @ClassName MessageServiceImpl.java
 * @createTime 2022年02月22日
 */
@Service
public class MessageServiceImpl extends BaseServiceImpl<Message> implements MessageService {
    private MessageMapper messageMapper;
    private MessageUserMapper messageUserMapper;

    @Autowired
    public MessageServiceImpl( MessageMapper messageMapper,MessageUserMapper messageUserMapper) {
        super(messageMapper);
        this.messageMapper = messageMapper;
        this.messageUserMapper = messageUserMapper;
    }

    @Override
    public PageInfo<Message> page(MessageRequestPageVM model) {
       return PageHelper.startPage(model.getPageIndex(), model.getPageSize(),"id desc").doSelectPageInfo(() ->
                messageMapper.listMessage(model)
        );
    }

    @Override
    public List<MessageUser> selectByMessageIds(List<Integer> ids) {
        return messageUserMapper.selectByMessageIds(ids);
    }

    @Override
    @Transactional
    public void sendMessage(Message message, List<MessageUser> messageUsers) {
        messageMapper.insertSelective(message);
        messageUsers.forEach(it -> it.setMessageId(message.getId()));
        messageUserMapper.inserts(messageUsers);
    }
}
